package com.noble.glamour.module.local.chat;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.noble.glamour.module.local.chat.utils.MsgUtils;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.socket.IMProxy;
import com.noble.glamour.module.logic.socket.NetData;
import com.noble.glamour.module.logic.socket.listener.SendCallBack;
import com.noble.library.observe.ModuleBase;
import com.noble.meether.chat.MessageConstant;
import com.noble.meether.chat.msgtype.CommonMessage;
import com.noble.meether.chat.msgtype.base.BaseMessage;
import com.noble.meether.chat.ui.imui.util.IMChatUtil;
import com.noble.meether.fragment.mail.listener.UpdateMsgListListener;

/**
 * 消息处理管理类
 */
public class ChatMgr implements ModuleBase {

    private RecMessageMgr messageMgr = new RecMessageMgr();

    @Override
    public void init() {
        messageMgr.init();
    }

    @Override
    public void release() {
        messageMgr.release();
    }

    private UpdateMsgListListener updateMsgListListener;

    // 更新信箱
    public void insertMailList(long whisperID, BaseMessage baseMessage) {
        if (updateMsgListListener == null) {
            updateMsgListListener = IMChatUtil.getInstance().getUpdateMsgListListener();
        }

        baseMessage.setUidFrom(whisperID);
        IMChatUtil.getInstance().updateMsgList(baseMessage);
    }

    // ======================================= 文字消息 =================================

    /**
     * @param isMsgList 是否更新mailFragment
     */
    public void sendTextMsg(long whisperID, @Nullable String content, boolean isMsgList, final SendCallBack sendCallBack) {
        final CommonMessage commonMessage = new CommonMessage(MessageConstant.MSG_ID_TEXT, content);
        commonMessage.setUidTo(whisperID);
        sendMessage(commonMessage, sendCallBack);
        if (isMsgList) {
            insertMailList(whisperID, commonMessage);
        }
    }

    // ======================================= 图片消息 =================================
    public void sendPhotoMsg(long whisperID, @Nullable String content, final SendCallBack sendCallBack) {
        final CommonMessage commonMessage = new CommonMessage(MessageConstant.MSG_ID_PIC, content);
        commonMessage.setUidTo(whisperID);
        sendMessage(commonMessage, sendCallBack);
        insertMailList(whisperID, commonMessage);
    }

    // ======================================= 小视频消息 =================================
    public void sendVideoMsg(long whisperID, @Nullable String video, String localVieo, String thumb, String localThumb, String duration, final SendCallBack sendCallBack) {
        final CommonMessage commonMessage = new CommonMessage(video, localVieo, thumb, localThumb, duration);
        commonMessage.setUidTo(whisperID);
        sendMessage(commonMessage, sendCallBack);
        insertMailList(whisperID, commonMessage);
    }

    // ======================================= 一对一音视频 =================================

    /**
     * @param msgType ： 消息类型 {@link com.noble.meether.chat.MessageConstant}
     */
    public void sendAVMsg(long whisperID, int msgType, SendCallBack sendCallBack) {
        CommonMessage commonMessage = new CommonMessage();
        commonMessage.setUidTo(whisperID);
        commonMessage.setMsgType(msgType);
        sendMessage(commonMessage, sendCallBack);
    }

    /**
     * 发送消息统一入口
     */
    public void sendMessage(final BaseMessage message, final SendCallBack sendCallBack) {
        // 构造消息发送体
        message.setUidFrom(App.uid);            // 需设置， 否则数据库插入失败
        NetData netData = new NetData();
        netData.setFromId(message.getLuidFrom());  // 发送方
        netData.setTuid(message.getUidTo());   // 接收方
        netData.setMsgType(message.getMsgType());      // 消息类型
        String content = message.getContent();
        if (TextUtils.isEmpty(content)) {
            content = "{}";
        }
        netData.setContent(content);   // 消息body体

        IMProxy.getInstance().send(netData, new SendCallBack() {
            @Override
            public void onResult(long msgId, long sender, String contents) {
                if (sendCallBack != null) {
                    sendCallBack.onResult(msgId, sender, contents);
                }
                insertDBMsg(message, 0);
            }

            @Override
            public void onSendFailed(NetData data) {
                if (sendCallBack != null) {
                    sendCallBack.onSendFailed(data);
                }
                insertDBMsg(message, 1);
            }
        });
    }

    public void insertDBMsg(BaseMessage message, int sendStatus) {
        if (!MsgUtils.isSaveMsg(message.getMsgType())) return;

        // 插入数据库
        message.setSendStatus(sendStatus);
        message.setIsSender(1);
        message.setWhisperId(message.getUidTo());
        ModuleMgr.getDbMgr().getMsgCache().insertMsg(message, null);
    }
}